package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC532 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int findPairs(int[] nums, int k) {
            Set<Integer> set = new HashSet<>();

            if (k == 0) {
                Set<Integer> moreThanTwiceSet = new HashSet<>();
                for (int num : nums) {
                    if (set.contains(num)) moreThanTwiceSet.add(num);
                    set.add(num);
                }
                return moreThanTwiceSet.size();
            }

            int count = 0;
            for (int num : nums) {
                if (set.contains(num)) continue;
                if (set.contains(num + k)) ++count;
                if (set.contains(num - k)) ++count;
                set.add(num);
            }

            return count;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,1,1,1,1]");
        System.out.println(solution.findPairs(arr, 0));
    }

    public static void main(String[] args) throws Exception {
        LC532 an = new LC532();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
